package com.zemb.flxwear.DB;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.zemb.flxwear.Bean.ReportItem;
import com.zemb.flxwear.Utils.DeviceUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//单例模式
public class DataBaseHelper {
    private String DATABASE_NAME = "FLX.db";
    private Context context;
    private FLXDatabase flxDatabase;
    private static DataBaseHelper dataBaseHelper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm");

    private DataBaseHelper(Context context) {
        this.context = context;
        flxDatabase = new FLXDatabase(context, DATABASE_NAME, null, 1);
    }

    public static DataBaseHelper getInstance(Context context) {
        if (dataBaseHelper == null) {
            synchronized (DataBaseHelper.class) {
                if (dataBaseHelper == null) {
                    dataBaseHelper = new DataBaseHelper(context);
                }
            }
        }
        return dataBaseHelper;
    }

    //添加血糖数据
    public void addBloodSugar(FourCategoryBean bloodSugar) {
        //如果不是血糖数据，直接返回
        if (bloodSugar.category != DeviceUtils.Category.BLOOD_SUGAR) {
            return;
        }
        flxDatabase.getWritableDatabase().execSQL("insert into Bloodsugar(uid,bloodSugarLevels,statue,date) values(?,?,?,?)",
                new Object[]{bloodSugar.uid, bloodSugar.data, bloodSugar.statue, bloodSugar.date});
    }

    //添加总胆固醇数据
    public void addCholesterol(FourCategoryBean cholesterol) {
        //如果不是总胆固醇数据，直接返回
        if (cholesterol.category != DeviceUtils.Category.CHOLESTEROL) {
            return;
        }
        flxDatabase.getWritableDatabase().execSQL("insert into Cholesterol(uid,totalCholesterol,statue,date) values(?,?,?,?)",
                new Object[]{cholesterol.uid, cholesterol.data, cholesterol.statue, cholesterol.date});
    }

    //添加尿酸数据
    public void addUricAcid(FourCategoryBean uricAcid) {
        //如果不是尿酸数据，直接返回
        if (uricAcid.category != DeviceUtils.Category.URIC_ACID) {
            return;
        }
        flxDatabase.getWritableDatabase().execSQL("insert into Uricacid(uid,uricAcid,statue,date) values(?,?,?,?)",
                new Object[]{uricAcid.uid, uricAcid.data, uricAcid.statue, uricAcid.date});

    }

    //添加血酮数据
    public void addBloodKetone(FourCategoryBean bloodKetone) {
        //如果不是血酮数据，直接返回
        if (bloodKetone.category != DeviceUtils.Category.BLOOD_KETONE) {
            return;
        }
        flxDatabase.getWritableDatabase().execSQL("insert into Bloodketone(uid,bloodKetone,statue,date) values(?,?,?,?)",
                new Object[]{bloodKetone.uid, bloodKetone.data, bloodKetone.statue, bloodKetone.date});
    }

    //添加血液四项数据
    public void addFourCategory(FourCategoryBean fourCategoryBean) {
        //如果不是血液四项数据，直接返回
        switch (fourCategoryBean.category) {
            case BLOOD_SUGAR:
                addBloodSugar(fourCategoryBean);
                break;
            case CHOLESTEROL:
                addCholesterol(fourCategoryBean);
                break;
            case URIC_ACID:
                addUricAcid(fourCategoryBean);
                break;
            case BLOOD_KETONE:
                addBloodKetone(fourCategoryBean);
                break;
            default:
                return;
        }
    }

    //添加血压数据
    public void addBloodPressure(BloodPressure bloodPressure) {
        try {
            flxDatabase.getWritableDatabase().execSQL("insert into Bloodpressure(uid,highPressure,lowPressure,statue,date) values(?,?,?,?,?)",
                    new Object[]{bloodPressure.uid, bloodPressure.highPressure, bloodPressure.lowPressure, bloodPressure.statue, bloodPressure.date});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //添加血氧数据
    public void addBloodOxygen(BloodOxygen bloodOxygen) {
        flxDatabase.getWritableDatabase().execSQL("insert into Bloodoxygen(uid,bloodOxygen,statue,date) values(?,?,?,?)",
                new Object[]{bloodOxygen.uid, bloodOxygen.bloodOxygen, bloodOxygen.statue, bloodOxygen.date});
    }

    //添加心电图数据
    public void addECG(ECG ecg) {
        flxDatabase.getWritableDatabase().execSQL("insert into ECG(uid,PR,other,EGCData,date) values(?,?,?,?,?)",
                new Object[]{ecg.uid, ecg.PR, ecg.other, ecg.EGCData, ecg.date});
    }

    //添加用户数据
    public void addUser(User user) {
        flxDatabase.getWritableDatabase().execSQL("INSERT INTO USER(type, name, phone, sex, age, userKey, date) VALUES (?, ?, ?, ?, ?, ?, ?)",
                new Object[]{user.type, user.name, user.phone, user.sex, user.age, user.userKey, user.date});

    }


//    //通过uid获取第一个血压数据
    public ReportItem queryBPFirstByUid(int uid) {
        try {
            Cursor cursor = flxDatabase.getWritableDatabase()
                    .query("Bloodpressure", null, "uid=?", new String[]{String.valueOf(uid)}, null, null, null);
            if (cursor.moveToLast()) {
                int highPressureIndex = cursor.getColumnIndex("highPressure");
                int lowPressureIndex = cursor.getColumnIndex("lowPressure");
                int dateIndex = cursor.getColumnIndex("date");
                return new ReportItem(
                        DeviceUtils.Category.BLOOD_PRESSURE,
                        "BP：" + cursor.getInt(highPressureIndex) + "/" + cursor.getInt(lowPressureIndex),
                        getTime(cursor.getLong(dateIndex))
                );
            }
        } catch (Exception e) {
            Log.e(DATABASE_NAME, "Error querying BP data for uid=" + uid, e);
        }
        return new ReportItem(DeviceUtils.Category.BLOOD_PRESSURE, "无数据", "-");
    }

    //通过uid获取第一个血氧数据
    public ReportItem queryBOFirstByUid(int uid) {
        try {
            Cursor cursor = flxDatabase.getWritableDatabase()
                    .query("Bloodoxygen", null, "uid=?", new String[]{String.valueOf(uid)}, null, null, null);
            if (cursor.moveToLast()) {
                int bloodOxygenIndex = cursor.getColumnIndex("bloodOxygen");
                int dateIndex = cursor.getColumnIndex("date");
                return new ReportItem(
                        DeviceUtils.Category.BLOOD_OXYGEN,
                        "BO：" + cursor.getDouble(bloodOxygenIndex),
                        getTime(cursor.getLong(dateIndex))
                );
            }
        } catch (Exception e) {
            Log.e(DATABASE_NAME, "Error querying BO data for uid=" + uid, e);
        }
        return new ReportItem(DeviceUtils.Category.BLOOD_OXYGEN, "无数据", "-");
    }

//  通过uid获取第一个血糖数据
    public ReportItem queryBSFirstByUid(int uid) {
        try {
            Cursor cursor = flxDatabase.getWritableDatabase()
                    .query("Bloodsugar", null, "uid=?", new String[]{String.valueOf(uid)}, null, null, null);
            if (cursor.moveToLast()) {
                int bloodSugarIndex = cursor.getColumnIndex("bloodSugar");
                int dateIndex = cursor.getColumnIndex("date");
                return new ReportItem(
                        DeviceUtils.Category.BLOOD_SUGAR,
                        "BS：" + cursor.getDouble(bloodSugarIndex),
                        getTime(cursor.getLong(dateIndex))
                );
            }
        } catch (Exception e) {
            Log.e(DATABASE_NAME, "Error querying BS data for uid=" + uid, e);
        }
        return new ReportItem(DeviceUtils.Category.BLOOD_SUGAR, "无数据", "-");
    }

    //通过uid获取第一个血酮数据
    public ReportItem queryBKFirstByUid(int uid){
        try {
            Cursor cursor = flxDatabase.getWritableDatabase()
                    .query("BloodKetone", null, "uid=?", new String[]{String.valueOf(uid)}, null, null, null);
            if (cursor.moveToLast()) {
                int bloodKetoneIndex = cursor.getColumnIndex("bloodKetone");
                int dateIndex = cursor.getColumnIndex("date");
                return new ReportItem(
                        DeviceUtils.Category.BLOOD_KETONE,
                        "BK：" + cursor.getDouble(bloodKetoneIndex),
                        getTime(cursor.getLong(dateIndex))
                );
            }
        } catch (Exception e) {
            Log.e(DATABASE_NAME, "Error querying BK data for uid=" + uid, e);
        }
        return new ReportItem(DeviceUtils.Category.BLOOD_KETONE, "无数据", "-");
    }



   //通过uid获取第一个尿酸数据
    public ReportItem queryUAFirstByUid(int uid) {
        try {
            Cursor cursor = flxDatabase.getWritableDatabase()
                    .query("UricAcid", null, "uid=?", new String[]{String.valueOf(uid)}, null, null, null);
            if (cursor.moveToLast()) {
                int uricAcidIndex = cursor.getColumnIndex("uricAcid");
                int dateIndex = cursor.getColumnIndex("date");
                return new ReportItem(
                        DeviceUtils.Category.URIC_ACID,
                        "UA：" + cursor.getDouble(uricAcidIndex),
                        getTime(cursor.getLong(dateIndex))
                );
            }
        } catch (Exception e) {
            Log.e(DATABASE_NAME, "Error querying UA data for uid=" + uid, e);
        }
        return new ReportItem(DeviceUtils.Category.URIC_ACID, "无数据", "-");
    }


//    //通过uid获取第一个胆固醇数据
    public ReportItem queryCHOLFirstByUid(int uid) {
        try (Cursor cursor = flxDatabase.getWritableDatabase()
                .query("Cholesterol", null, "uid=?", new String[]{String.valueOf(uid)}, null, null, null)) {
            if (cursor.moveToLast()) {
                int cholesterolIndex = cursor.getColumnIndex("cholesterol");
                int dateIndex = cursor.getColumnIndex("date");
                return new ReportItem(
                        DeviceUtils.Category.CHOLESTEROL,
                        "CH：" + cursor.getDouble(cholesterolIndex),
                        getTime(cursor.getLong(dateIndex))
                );
            }
        } catch (Exception e) {
            Log.e(DATABASE_NAME, "Error querying CHOL data for uid=" + uid, e);
        }
        return new ReportItem(DeviceUtils.Category.CHOLESTEROL, "无数据", "-");
    }

    //通过uid获取第一个心电数据
    public ReportItem queryECGFirstByUid(int uid){
        try (Cursor cursor = flxDatabase.getWritableDatabase()
                .query("ECG", null, "uid=?", new String[]{String.valueOf(uid)}, null, null, null)) {
            if (cursor.moveToLast()) {
                int ecgIndex = cursor.getColumnIndex("EGCData");
                int dateIndex = cursor.getColumnIndex("date");
                return new ReportItem(
                        DeviceUtils.Category.ECG,
                        "ECG：" + cursor.getString(ecgIndex),
                        getTime(cursor.getLong(dateIndex))
                );
            }
        } catch (Exception e) {
            Log.e(DATABASE_NAME, "Error querying ECG data for uid=" + uid, e);
        }
        return new ReportItem(DeviceUtils.Category.CHOLESTEROL, "无数据", "-");
    }





    //对应表结构的对象 用户
    public static class User {
        private int id;
        private int type;
        private String name;
        private String phone;
        private int sex;
        private int age;
        private String userKey;
        private long date;

        public User() {
        }

        public User(int type, String name, String phone, int sex, int age, String userKey, long date) {
            this.type = type;
            this.name = name;
            this.phone = phone;
            this.sex = sex;
            this.age = age;
            this.userKey = userKey;
            this.date = date;
        }

        // 省略 getter 和 setter 方法
    }


    //对应表结构的对象 血液四项
    public static class FourCategoryBean {
        public DeviceUtils.Category category;
        public int id;
        public int uid;
        public double data;
        public String statue;
        public long date;
    }

    //对应表结构的对象 血压
    public static class BloodPressure {
        public int id;
        public int uid;
        public double highPressure;
        public double lowPressure;
        public int plus;
        public String statue;
        public long date;
    }

    //对应表结构的对象 血氧
    public static class BloodOxygen {
        public int id;
        public int uid;
        public double bloodOxygen;
        public int plus;
        public String statue;
        public long date;
    }

    //对应表结构的对象 心电图
    public static class ECG {
        public int id;
        public int uid;
        public int PR;
        public String other;
        public String EGCData;
        public long date;
    }


    public String getTime(Long date) {
        return sdf.format(new Date(date));
    }
}
